﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Gra.Seguros.Servicio.Be;


namespace Gra.Seguros.Servicio.Dalc
{
    public class PlanSeguroDalc: IDisposable
    {
        #region "Procedimientos Consultas"

        public List<PlanSeguroBe> listar()
        {
            try
            {
                List<PlanSeguroBe> lst = new List<PlanSeguroBe>();
                using (SqlCommand oCommand = new SqlCommand("uspListarPlanSeguro", oCn))
                {
                    oCommand.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader drListado = oCommand.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection))
                    {
                        if (drListado.HasRows)
                        {
                            while ((drListado.Read()))
                            {
                                PlanSeguroBe obj = new PlanSeguroBe();
                                obj.cPlan = drListado.GetInt32(drListado.GetOrdinal("cPlan"));
                                obj.dPlan = drListado.GetString(drListado.GetOrdinal("dPlan"));
                                lst.Add(obj);
                            }
                        }
                    }
                }

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<PlanSeguroBe> listarPlanSeguroxRamo(int _cAseguradora,int _cRamo)
        {
            try
            {
                List<PlanSeguroBe> lst = new List<PlanSeguroBe>();
                using (SqlCommand oCommand = new SqlCommand("uspListarPlanSeguroxRamo", oCn))
                {
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@cAseguradora", SqlDbType.Int).Value = _cAseguradora;
                    oCommand.Parameters.Add("@cRamo", SqlDbType.Int).Value = _cRamo;
                    using (SqlDataReader drListado = oCommand.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection))
                    {
                        if (drListado.HasRows)
                        {
                            while ((drListado.Read()))
                            {
                                PlanSeguroBe obj = new PlanSeguroBe();
                                obj.cPlan = drListado.GetInt32(drListado.GetOrdinal("cPlan"));
                                obj.dPlan = drListado.GetString(drListado.GetOrdinal("dPlan"));
                                lst.Add(obj);
                            }
                        }
                    }
                }

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region "Constructor & Disposable"
        private bool _disposed = false;
        SqlConnection oCn = null;

        public PlanSeguroDalc()
        {
            string strConnection = ConfigurationManager.ConnectionStrings["dbSeguros"].ConnectionString;
            oCn = new SqlConnection(strConnection);
            oCn.Open();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    //Dispose managed resources here. llamar componetes de conexion u otros.                        
                    if (oCn.State != ConnectionState.Closed)
                    {
                        oCn.Close();
                    }
                    oCn.Dispose();
                }
                //Disposed unmanaged resources here.
                //set the _disposed flag to prevent subsequent disposals.
                _disposed = true;
            }
        }
        //Finalization code.
        ~PlanSeguroDalc()
        {
            Dispose(false);
        }
        #endregion
    }

}
